package cn.fintecher.pangolin.service.business.web;

import cn.fintecher.pangolin.common.enums.DistributeType;
import cn.fintecher.pangolin.common.exception.BadRequestException;
import cn.fintecher.pangolin.common.model.UserModel;
import cn.fintecher.pangolin.common.web.BaseController;
import cn.fintecher.pangolin.service.business.model.request.DistributeAgrsRequest;
import cn.fintecher.pangolin.service.business.model.response.CaseCountAndTotalResponse;
import cn.fintecher.pangolin.service.business.model.response.CaseDistributePreResponse;
import cn.fintecher.pangolin.service.business.model.response.DistributeStatisticalResponse;
import cn.fintecher.pangolin.service.business.service.BaseService;
import cn.fintecher.pangolin.service.business.service.DistributeCaseService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author wangxiangdong
 * @Description 案件分配
 * @Date 2019/9/3 17:52
 **/
@RestController
@RequestMapping("/api/caseInfoDistributeController")
@Api(value = "案件分配", description = "案件分配")
public class CaseInfoDistributeController extends BaseController {

    @Autowired
    private DistributeCaseService distributeCaseService;

    @Autowired
    private BaseService baseService;

    @PostMapping("/caseDistributePreview")
    @ApiOperation(value = "待分配手动分案预览", notes = "待分配手动分案预览")
    public ResponseEntity<CaseDistributePreResponse> caseDistributePreview(@RequestBody DistributeAgrsRequest request) throws BadRequestException {
        CaseDistributePreResponse response = distributeCaseService.caseDistributePreview(request);
        return ResponseEntity.ok().body(response);
    }

    @PostMapping("/orgCasesDistributePreview")
    @ApiOperation(value = "机构待分配手动分案预览", notes = "机构待分配手动分案预览")
    public ResponseEntity<CaseDistributePreResponse> orgCasesDistributePreview(@RequestBody DistributeAgrsRequest request) throws BadRequestException {
        CaseDistributePreResponse response = distributeCaseService.orgCaseDistributePreview(request);
        return ResponseEntity.ok().body(response);
    }


    @PostMapping("/waitCaseDistributeConfirm")
    @ApiOperation(value = "待分配手动分案确认", notes = "待分配手动分案确认")
        public ResponseEntity waitCaseDistributeConfirm(@RequestBody CaseDistributePreResponse request,@RequestHeader(value = "X-UserToken") String token) throws BadRequestException {
        UserModel userByToken = baseService.getUserByToken(token);
        if(request.getDistributeType().name().equals(DistributeType.DEPARTMENT.name())){
            distributeCaseService.confirmDistributeCaseForDepartment(request, userByToken);
        }else{
            distributeCaseService.confirmDistributeCaseForCollector(request, userByToken);
        }
        return ResponseEntity.ok().build();
    }

    @PostMapping("/distributedCasesConfirm")
    @ApiOperation(value = "机构待分配、在催案件池分案确认", notes = "机构待分配、在催案件池分案确认")
    public ResponseEntity distributedCasesConfirm(@RequestBody CaseDistributePreResponse request,@RequestHeader(value = "X-UserToken") String token) throws BadRequestException {
        UserModel userByToken = baseService.getUserByToken(token);
        if(request.getDistributeType().name().equals(DistributeType.DEPARTMENT.name())){
            distributeCaseService.orgConfirmCase(request.getDistributeConfigModels(), userByToken);
        }else{
            distributeCaseService.collectorConfirmCases(request.getDistributeConfigModels(), userByToken);
        }
        return ResponseEntity.ok().build();
    }

    @GetMapping("/queryCountAndTotalByBatchNumber")
    @ApiOperation(value = "根据batchNumber查询案件数量和金额", notes = "根据batchNumber查询案件数量和金额")
    public ResponseEntity<CaseCountAndTotalResponse> queryCountAndTotalByBatchNumber(@RequestParam String batchNumber) throws BadRequestException {
        return ResponseEntity.ok().body(distributeCaseService.queryCountAndTotalByBatchNumber(batchNumber));
    }

    @GetMapping("/getWaitBatchNumberAndTotal")
    @ApiOperation(value = "获取待分配可分配的批次号", notes = "获取待分配可分配的批次号")
    public ResponseEntity<List<DistributeStatisticalResponse>> getWaitBatchNumberAndTotal(@RequestHeader(value = "X-UserToken") String token) throws BadRequestException {
        UserModel userModel = baseService.getUserByToken(token);
        return ResponseEntity.ok().body(distributeCaseService.getWaitBatchNumberAndTotal(userModel));
    }

    @GetMapping("/getSettleBatchNumber")
    @ApiOperation(value = "获取结清案件的批次号", notes = "获取结清案件的批次号")
    public ResponseEntity<List<String>> getSettleBatchNumber(@RequestHeader(value = "X-UserToken") String token) throws BadRequestException {
        UserModel userModel = baseService.getUserByToken(token);
        return ResponseEntity.ok().body(distributeCaseService.getSettleBatchNumber(userModel));
    }

    @GetMapping("/getBatchNumberAndTotal")
    @ApiOperation(value = "获取批次号以及金额统计", notes = "获取批次号以及金额统计")
    public ResponseEntity<List<DistributeStatisticalResponse>> getBatchNumberAndTotal(String distributePoolType,@RequestHeader(value = "X-UserToken") String token) throws BadRequestException {
        UserModel userModel = baseService.getUserByToken(token);
        return ResponseEntity.ok().body(distributeCaseService.getBatchNumberAndTotal(distributePoolType,userModel));
    }
}
